
package com.cxhd.lis.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 日期工具类
 *
 * @author liyulin lyl010991@126.com @date 2014/11/29
 * @version 1.0
 */
public class DateUtil {

    /**
     * 将Date转化为毫秒
     *
     * @param date
     * @return
     */
    public static long dateToMillis(java.util.Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        return c.getTimeInMillis();
    }

    /**
     * 将毫秒转化为Date
     *
     * @param time
     * @return
     */
    public static Date millisToDate(long time) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTimeInMillis(time);
        return c.getTime();
    }

    /**
     * 返回在Date的的基础上加上minute分钟后的Date
     *
     * @param date 日期
     * @param minite 分钟
     * @return
     */
    public static Date addDate(String dateText, int minite) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse(dateText);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return millisToDate(dateToMillis(date) + minite * 1000 * 60);
    }

    /**
     * 日期转字符串
     *
     * @param timestamp
     * @return
     */
    public static String dateFormat(Timestamp timestamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(timestamp);
    }

    /**
     * 日期转字符串
     *
     * @param date
     * @return
     */
    public static String dateFormat(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public static String dateFormat2(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    /**
     * 从身份证中提取出生日期
     *
     * @param certificate 身份证
     * @return yy-MM-dd
     */
    public static String getBirthdayFromCertificate(String certificate) {
        StringBuilder sb = new StringBuilder();
        sb.append(certificate.substring(6, 10));
        sb.append("-");
        sb.append(certificate.substring(10, 12));
        sb.append("-");
        sb.append(certificate.substring(12, 14));

        return sb.toString();
    }

    /**
     * 获取身份证后10位数
     *
     * @param certificate 身份证
     * @return
     */
    public static String getCertificate10(String certificate) {
        return certificate.substring(8);
    }

    /**
     * 返回1天后的日期
     *
     * @param date 日期
     * @return
     */
    public static Date after1Day(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 1);
        return calendar.getTime();
    }

    /**
     * 返回2天后的日期
     *
     * @param date 日期
     * @return
     */
    public static Date after2Day(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 2);
        return calendar.getTime();
    }

    /**
     * 返回3天后的日期
     *
     * @param date 日期
     * @return
     */
    public static Date after3Day(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 3);
        return calendar.getTime();
    }

    /**
     * 返回7天后的日期
     *
     * @param date 日期
     * @return
     */
    public static Date after7Day(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 7);
        return calendar.getTime();
    }

    /**
     * 返回30天后的日期
     *
     * @param date 日期
     * @return
     */
    public static Date after30Day(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 30);
        return calendar.getTime();
    }

    /**
     * 返回45天后的日期
     *
     * @param date 日期
     * @return
     */
    public static Date after45Day(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, 45);
        return calendar.getTime();
    }

    /**
     * 返回一个月后的日期
     *
     * @param date 日期
     * @return
     */
    public static Date after1Month(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.MONTH, 1);
        return calendar.getTime();
    }

    /**
     * 返回2个月后的日期
     *
     * @param date 日期
     * @return
     */
    public static Date after2Month(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.MONTH, 2);
        return calendar.getTime();
    }

    /**
     * 返回三个月后的日期
     *
     * @param date 日期
     * @return
     */
    public static Date after3Month(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.MONTH, 3);
        return calendar.getTime();
    }

    /**
     * 返回4个月后的日期
     *
     * @param date 日期
     * @return
     */
    public static Date after4Month(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.MONTH, 4);
        return calendar.getTime();
    }

    /**
     * 返回六个月后的日期
     *
     * @param date 日期
     * @return
     */
    public static Date after6Month(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.MONTH, 6);
        return calendar.getTime();
    }

    /**
     * 返回九个月后的日期
     *
     * @param date 日期
     * @return
     */
    public static Date after9Month(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.MONTH, 9);
        return calendar.getTime();
    }

    /**
     * 返回十二个月后的日期
     *
     * @param date 日期
     * @return
     */
    public static Date after12Month(Date date) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.MONTH, 12);
        return calendar.getTime();
    }

    public static void main(String[] args) {
        try {
            //        Date d = new Date();
//        System.out.println(after1Day(d));
//        System.out.println(after1Month(d));
//        System.out.println(after3Month(d));
//        System.out.println(after6Month(d));
//        System.out.println(after9Month(d));
//        System.out.println(after12Month(d));
System.out.println(getNumToDate("20161028091112"));

            //.out.println(getCurrentAgeByBirthdate("1989-09-11"));
        } catch (Exception ex) {
            Logger.getLogger(DateUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public static String getNumToDate(String date){
    
        try {
            String datestr = "20110219";
            
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date strToDate = sdf.parse(date);
            String date1 = format1.format(strToDate);
            return date1;// 2012-10-03 23:41:31
        } catch (ParseException ex) {
           
        }
        return null;
    }

    public static String getDateCN() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        String date = format.format(new Date(System.currentTimeMillis()));
        return date;
    }

    public static String getDateEN() {
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date1 = format1.format(new Date(System.currentTimeMillis()));
        return date1;// 2012-10-03 23:41:31
    }

    public static String getDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date = format.format(new Date(System.currentTimeMillis()));
        return date;
    }
      public static String getDate2() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        String date = format.format(new Date(System.currentTimeMillis()));
        return date;
    }

    public static String getNum() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String date = format.format(new Date(System.currentTimeMillis()));
        return date;
    }

    public static String getTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String date = format.format(new Date(System.currentTimeMillis()));
        return date;
    }

    /**
     * @author jerry.chen
     * @param brithday
     * @return
     * @throws ParseException 根据生日获取年龄;
     */
    public static int getCurrentAgeByBirthdate(String brithday) {
        try {
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
            String currentTime = formatDate.format(calendar.getTime());
            Date today = formatDate.parse(currentTime);
            Date brithDay = formatDate.parse(brithday);

            return today.getYear() - brithDay.getYear();
        } catch (Exception e) {
            return 0;
        }
    }
}
